function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

/**
 * 文件上传
 * */
function upload(ele) {
    if (fileName()) {
        if (fileMax(ele)) {
            $("#addClass").submit();
        }
    }

}

/**
 * 验证文件后缀
 * @returns {boolean}
 */
function fileName() {
    var location = $("input[name='file']").val();
    var point = location.lastIndexOf(".");
    var type = location.substr(point);
    if (type == ".jpg" || type == ".gif" || type == ".png" || type == ".jpeg") {
        return true;

    } else {
        layer.msg('文件后缀不正确', {icon: 3});
        false;
    }
}

/**
 * 验证文件大小
 * @returns {boolean}
 */
function fileMax(ele) {
    var maxsize = 1024 * 1024;//2M
    var errMsg = "上传的附件文件不能超过2M！！！";
    var tipMsg = "您的浏览器暂不支持计算上传文件的大小，确保上传文件不要超过2M，建议使用IE、FireFox、Chrome浏览器。";
    var browserCfg = {};
    var ua = window.navigator.userAgent;
    if (ua.indexOf("MSIE") >= 1) {
        browserCfg.ie = true;
    } else if (ua.indexOf("Firefox") >= 1) {
        browserCfg.firefox = true;
    } else if (ua.indexOf("Chrome") >= 1) {
        browserCfg.chrome = true;
    }
    try {
        var obj_file = document.getElementById("uploadfile");
        if (obj_file.value == "") {
            alert("请先选择上传文件");
            return false;
        }
        var filesize = 0;
        if (browserCfg.firefox || browserCfg.chrome) {
            filesize = obj_file.files[0].size;
        } else if (browserCfg.ie) {
            var obj_img = document.getElementById('tempimg');
            obj_img.dynsrc = obj_file.value;
            filesize = obj_img.fileSize;
        } else {
            layer.msg(tipMsg, {icon: 4});
            return false;
        }
        if (filesize == -1) {
            layer.msg(tipMsg, {icon: 4});
            return false;
        } else if (filesize > maxsize) {
            layer.msg(errMsg, {icon: 4});
            return false;
        } else {
            return true;
        }
    } catch (e) {
        alert(e);
    }

}

$(document).ready(function () {

    $("#uploadfile").change(function () {
        if (this.files.length == 1) {
            if (fileName()) {
                var objUrl = getObjectURL(this.files[0]);
                if (objUrl) {
                    $("#image").attr("src", objUrl);
                }

            }

        } else {
            layer.msg('最多只能选择一张图片', {icon: 4});
        }

    });
    var bool = $("#bool").text();
    if (bool == "true") {
        layer.msg('添加成功', {icon: 1});
        $("#bool").text("false");
    }

});

function typeChange(ele) {
    var value = ele.value;
    var url = $("#url").text() + "softWareClassifyAction/admin/menu?param=" + value;
    $.get(url, function (data) {
            //清空select的值
            $("#fidType").empty();
            //添加select的值
            $("#fidType").append("<option value=" + value + ">一级菜单</option>");
            $.each(data, function (index, obj) {
                $("#fidType").append("<option value=" + obj[0] + ">" + obj[1] + "</option>");
            });

        }, "json"
    );
}



